package com.tanxi521.afkreward.command;

import com.tanxi521.afkreward.data.AFKRewardMapper;
import com.tanxi521.afkreward.util.ColorUtil;
import com.tanxi521.afkreward.util.CommandUtil;
import com.tanxi521.afkreward.util.ConfigUtil;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AfkCommand implements CommandExecutor, TabCompleter {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (args.length == 0) {
            sender.sendMessage("§e输入/tna help获取插件帮助");
            return true;
        }
        if (args.length == 1) {
            if (args[0].equals("help")) {
                List<String> pluginHelp = ConfigUtil.getPluginHelp();
                String rest = ConfigUtil.isMaxLimit() ? (ConfigUtil.getMaxRest() - AFKRewardMapper.getRest(sender.getName()) + "") : "∞";
                String num = ConfigUtil.isPlayerLimit() ? (ConfigUtil.getMaxNum() - AFKRewardMapper.AFK_MAP.size() + "") : "∞";
                final List<String> res = new ArrayList<>();
                pluginHelp.forEach(s -> res.add(ColorUtil.replace(s).replace("%min%", ConfigUtil.getRound() + "")
                        .replace("%num%", num).replace("%rest%", rest)));
                sender.sendMessage(res.toArray(new String[0]));
                return true;
            } else if (args[0].equals("start")) {
                return CommandUtil.freeAfkList(sender);
            } else if (args[0].equals("end")) {
                return CommandUtil.stopAfk(sender);
            } else if (args[0].equals("list")) {
                return CommandUtil.afkList(1, sender);
            } else if (args[0].equals("top")) {
                return CommandUtil.top(sender, 1);
            } else if (args[0].equals("reload")) {
                return CommandUtil.reload(sender);
            }
        }
        if (args.length == 2) {
            if (args[0].equals("start")) return CommandUtil.startFreeAfk(sender, args[1]);
            if (args[0].equals("area") && args[1].equals("start")) return CommandUtil.startAreaAfk(sender);
            if (args[0].equals("list") && ColorUtil.isNumeric(args[1]))
                return CommandUtil.afkList(Integer.parseInt(args[1]), sender);
            if (args[0].equals("top") && ColorUtil.isNumeric(args[1]))
                return CommandUtil.top(sender, Integer.parseInt(args[1]));
        }
        sender.sendMessage(ColorUtil.replace(ConfigUtil.langYml.getString("uncorrect")));
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if (args.length == 1) {
            String[] cmds = {"start", "end", "list", "top", "area", "reload"};
            return Arrays.stream(cmds).filter(s -> s.startsWith(args[0])).collect(Collectors.toList());
        }
        if (args.length == 2 && args[0].equals("area")) {
            String[] cmds = {"start"};
            return Arrays.stream(cmds).filter(s -> s.startsWith(args[0])).collect(Collectors.toList());
        }
        return null;
    }
}
